import { BizKfExtraProColumnType } from '@/components';
import * as API from '@/services';
import { Divider, message, Space, Typography } from 'antd';
import { KfModal, KfPagingTablePage, KfPagingTablePagePropsAction, useBackendEnum } from 'kf-web-library';
import React, { useRef } from 'react';
import { useLocation } from 'umi';
import QuickForm from './components/QuickForm';
import { useAccess } from '@@/exports';

type Entity = API.Role;
type EntityPageQueryParams = API.RolePageQueryParams;
const entityTypeName = '角色';
const EntityQuickForm = QuickForm;

interface EntityTableListProps {}

/**
 * 角色列表
 */
const EntityTableList: React.FC<EntityTableListProps> = (props) => {
  const location = useLocation();
  const access = useAccess();
  const { enumDict, enumTypeDict, getBackendEnum } = useBackendEnum();
  const actionRef = useRef<KfPagingTablePagePropsAction<Entity, EntityPageQueryParams>>();

  return (
    <KfPagingTablePage<Entity, EntityPageQueryParams, BizKfExtraProColumnType>
      actionRef={actionRef}
      entityTypeName={entityTypeName}
      pagingQuery={API.queryRoles}
      opActionsDef={{
        single: {
          deleteOne: (record) => {
            KfModal.deleteConfirm(record.id as string, () => {
              API.deleteRole(record.id!).then((res) => {
                if (res.flag) {
                  message.info('删除成功');
                  actionRef.current?.reload();
                }
              });
            });
          },
        },
        multi: {
          deleteMultiple: (records) => {
            KfModal.batchActionConfirm('删除', records.length, () => {
              API.deleteRoles(records.map((r) => r.id!)).then((res) => {
                if (res.flag) {
                  message.info('批量删除成功');
                  actionRef.current?.reload();
                }
              });
            });
          },
        },
      }}
      opFloatingsDef={{
        noargs: {
          createModal: () => ({
            title: `新建${entityTypeName}`,
            content: <EntityQuickForm formType={'create'} />,
          }),
        },
        single: {
          modifyModal: (record) => ({
            title: `编辑${entityTypeName}`,
            content: <EntityQuickForm formType={'modify'} bizId={record.id} />,
          }),
          viewModal: (record) => ({
            fltType: 'drawer',
            title: `查看${entityTypeName}`,
            content: <EntityQuickForm formType={'view'} bizId={record.id} />,
          }),
        },
      }}
      proTable={{
        tableBarActions: (action, rows) => {
          return [
            /* <KfPagingTableExport oneSettingIgnoreDropdown={false} settings={[DEFAULT_SCOPE_CURRENT_PAGE_EXPORT_SETTING, DEFAULT_SCOPE_ALL_EXPORT_SETTING]}/>,*/
          ];
        },
        search: {
          optionRender: (searchConfig, formProps, dom) => [
            ...dom,

            /*<KfMultipleMenuExportCom
                key={"export"}
                fetchApiFun={roleService.queryRoles}
                reqPath={'/api/v1/role'}
                qsParams={{...qs.parse(location.search)}}
                reqParams={{
                    _export_scope_: 'ALL',
                    _export_type_: 'EXCEL',
                }}
            />,*/
          ],
        },
      }}
      columns={[
        {
          title: '关键字',
          dataIndex: 'searchText',
          sorter: false,
          hideInTable: true,
          hideInForm: true,
          fieldProps: {
            placeholder: '模糊名称和编码',
          },
        },
        {
          title: '角色编码',
          dataIndex: nameof<Entity>((x) => x.code),
          width: 200,
          sorter: false,
          hideInSearch: true,
          ellipsis: true,
        },
        {
          title: '角色名称',
          dataIndex: nameof<Entity>((x) => x.name),
          sorter: false,
          hideInSearch: true,
        },
        {
          title: '备注',
          width: 300,
          ellipsis: true,
          dataIndex: nameof<Entity>((x) => x.description),
          hideInSearch: true,
        },
        {
          title: '操作',
          fixed: 'right',
          width: 130,
          dataIndex: 'option',
          valueType: 'option',
          render: (_, record) => (
            <Space  size={0} split={<Divider type="vertical" />}>
              <a onClick={() => actionRef.current?.openFloating.single?.viewModal(record)}>查看</a>

              {access.hasAnyAuthority('ROLE_ADMIN', 'ROLE_NODE_ADMIN') && (
                <>
                  <a onClick={() => actionRef.current?.openFloating.single?.modifyModal(record, 'drawer')}>编辑</a>
                  <a onClick={() => actionRef.current?.executeAction.single?.deleteOne(record)}>
                    <Typography.Text type="danger">删除</Typography.Text>
                  </a>
                </>
              )}
            </Space>
          ),
        },
      ]}
    />
  );
};

export default EntityTableList;
